package com.seecen.webproject.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.seecen.webproject.bo.EmpBO;
import com.seecen.webproject.bo.Page;
import com.seecen.webproject.entity.Emp;
import com.seecen.webproject.service.EmpService;
import com.seecen.webproject.vo.Result;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/emp/list")
@Log4j
public class EmpPageController extends HttpServlet {


    private EmpService empService = new EmpService();
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<List<Emp>, EmpBO> page = new Page<>();
        EmpBO empBO = new EmpBO();
        empBO.getEmp().setEname(request.getParameter("ename"));
        empBO.setQueryDate1(request.getParameter("queryDate1"));
        empBO.setQueryDate2(request.getParameter("queryDate2"));
        page.setQueryBO(empBO);
        // 要显示的页数，默认第1页
        page.setPageNo(request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo")));
        // 要每页显示的条数，默认2条
        page.setPageCount(request.getParameter("pageCount") == null ? 2 : Integer.parseInt(request.getParameter("pageCount")));



        empService.page(page);

        Result result = Result.success(page);
        String jsonStr = objectMapper.writeValueAsString(result);
        response.getWriter().println(jsonStr);
    }
}
